package 代码记录.指定公司代码练习记录.备战快手;

/**
 * @author zx
 * @create 2023-05-13 12:15
 */
public class 最长回文子串 {
    /**
     * 再做一遍
     */
    public String longestPalindrome(String s) {
        int left = 0,right = 0;
        for(int i = 0;i < s.length();i++){
            int len = Math.max(extend(s,i,i),extend(s,i,i + 1));
            if(len > right - left){
                left = i - (len - 1) / 2;
                right = i + len / 2;
            }
        }
        return s.substring(left,right + 1);
    }
    private int extend(String s, int i, int j){
        while(i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j)){
            i--;
            j++;
        }
        return j - i - 1;
    }
}
